//
//  CZRefreshView.swift
//  自定义刷新控件
//
//  Created by Summer on 2017/12/15.
//  Copyright © 2017年 Summer. All rights reserved.
//

import UIKit

/// 刷新视图
class CZRefreshView: UIView {

    /// 刷新状态
    /*
     iOS 系统中，UIview封装的动画
     - 默认顺时针旋转
     - 就近原则
     需求：要向实现同方向旋转，需要调整一个非常小的数字（就近原则）
     如果要实现360°旋转，需要使用核心动画CABaseAnimation
     */
    var refreshState: CZRefreshState = .normal {
        didSet {
            switch refreshState {
            case .normal:
                // 恢复初始状态
                tipIcon?.isHidden = false
                indicator?.stopAnimating()
                
                tipLabel?.text = "下拉刷新..."
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon?.transform = CGAffineTransform.identity
                })
            case .pulling:
                tipLabel?.text = "松手刷新..."
                // 图像旋转 180°    M_PI
                // 箭头在左边旋转 - 0.001
                // 箭头在右边旋转 + 0.001
                UIView.animate(withDuration: 0.25, animations: {
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi - 0.001))
                })
                
            case .willRefresh:
                tipLabel?.text = "正在刷新..."
                
                // 隐藏提示图标
                tipIcon?.isHidden = true
                
                // 显示菊花
                indicator?.startAnimating()
            }
        }
    }
    /// 指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    /// 提示图标
    @IBOutlet weak var tipIcon: UIImageView?
    /// 提示标签
    @IBOutlet weak var tipLabel: UILabel?
    /// view高度
    var viewHeight: CGFloat = 0
    
    // 从XIB加载，使用类方法
    class func refreshView() -> CZRefreshView {
        let nib = UINib(nibName: "CZGifRefreshView", bundle: nil)
        return nib.instantiate(withOwner: nil, options: nil)[0] as! CZRefreshView
    }
}
